# -*- coding: utf-8 -*-
# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response,redirect

from LyricStructure.pylyric.LyricSA import analysis,analysis1,analysis2
from LyricStructure.pysplit.LyricSA2 import analysis as rseg_analysis
from LyricStructure.pylyric.LyricSN import RUStructName
from LyricStructure.pylyric.Lyric import Lyric
curanalysis = analysis2


def home(request):
    return render_to_response("lyricstructure/index.html")

def ruc(request):
    if request.method == "GET":
        #the page with a form 
        return render_to_response("lyricstructure/ruc.html",{})

    if request.method == "POST":
        #each seg in a block on the right side
        #result is a list of line list
        #each line is a dic has a time and a content
        dic = {}
        dic['result'] = []
        content = request.POST['content']
        dic['textareavalue'] = content
        content = content.encode("utf-8")
        
        l = Lyric()
        l.loadstr(content)
        linelist = l.getlinelist()

        structs = analysis(l)
        result = []
        for struct in structs:
            tmplist = []
            for i in range(struct[0],struct[1]+1):
                tmpdic = {}
                tmpdic['time'] = linelist[i]['time']
                tmpdic['content'] = linelist[i]['content']
                tmplist.append(tmpdic)
            result.append(tmplist)
        dic['result'] = result
        return render_to_response("lyricstructure/ruc.html",dic)

def ruc1(request):
    if request.method == "GET":
        #the page with a form 
        return render_to_response("lyricstructure/ruc1.html",{})

    if request.method == "POST":
        #each seg in a block on the right side
        #result is a list of line list
        #each line is a dic has a time and a content
        dic = {}
        dic['result'] = []
        content = request.POST['content']
        dic['textareavalue'] = content
        content = content.encode("utf-8")
        
        l = Lyric()
        l.loadstr(content)
        linelist = l.getlinelist()

        structs = analysis1(l)
        result = []
        for struct in structs:
            tmplist = []
            for i in range(struct[0],struct[1]+1):
                tmpdic = {}
                tmpdic['time'] = linelist[i]['time']
                tmpdic['content'] = linelist[i]['content']
                tmplist.append(tmpdic)
            result.append(tmplist)
        dic['result'] = result
        return render_to_response("lyricstructure/ruc1.html",dic)

def ruc2(request):
    if request.method == "GET":
        #the page with a form 
        return render_to_response("lyricstructure/ruc2.html",{})

    if request.method == "POST":
        #each seg in a block on the right side
        #result is a list of line list
        #each line is a dic has a time and a content
        dic = {}
        dic['result'] = []
        content = request.POST['content']
        dic['textareavalue'] = content
        content = content.encode("utf-8")
        
        l = Lyric()
        l.loadstr(content)
        linelist = l.getlinelist()

        structs = analysis2(l)
        result = []
        for struct in structs:
            tmplist = []
            for i in range(struct[0],struct[1]+1):
                tmpdic = {}
                tmpdic['time'] = linelist[i]['time']
                tmpdic['content'] = linelist[i]['content']
                tmplist.append(tmpdic)
            result.append(tmplist)
        dic['result'] = result
        return render_to_response("lyricstructure/ruc2.html",dic)

def rseg(request):
    if request.method == "GET":
        #the page with a form 
        return render_to_response("lyricstructure/rseg.html",{})

    if request.method == "POST":
        dic = {}
        dic['result'] = []
        content = request.POST['content']
        dic['textareavalue'] = content
        content = content.encode("utf-8")
        
        l = Lyric()
        l.loadstr(content)
        linelist = l.getlinelist()

        structs = rseg_analysis(l)
        result = []
        for struct in structs:
            tmplist = []
            for i in range(struct[0],struct[1]+1):
                tmpdic = {}
                tmpdic['time'] = linelist[i]['time']
                tmpdic['content'] = linelist[i]['content']
                tmplist.append(tmpdic)
            result.append(tmplist)
        dic['result'] = result
        return render_to_response("lyricstructure/rseg.html",dic)



def sn1(request):
    """
    structure naming
    @see pylyric LyricSA LyricSN
    """
    if request.method == "GET":
        #the page with a form 
        return render_to_response("lyricstructure/ruc_name1.html",{})

    if request.method == "POST":
        #each seg in a block on the right side
        #result is a list of line list
        #each line is a dic has a time and a content
        dic = {}
        dic['result'] = []
        content = request.POST['content']
        dic['textareavalue'] = content
        content = content.encode("utf-8")
        
        l = Lyric()
        l.loadstr(content)
        linelist = l.getlinelist()

        structs = curanalysis(l)
        segstype = RUStructName(l,structs)
        colordic = {
                "chorus":"#ff0000",
                "verse":"#ffff00",
                "ending":"#00ff00",
                "unknown":"#ffffff",
                }
        result = []
        for i in range(len(structs)):
            segdic = {}
            segdic['type'] = segstype[i]
            segdic['color'] = colordic[segstype[i]]
            tmplist = []
            for j in range(structs[i][0],structs[i][1]+1):
                tmpdic = {}
                tmpdic['time'] = linelist[j]['time']
                tmpdic['content'] = linelist[j]['content']
                tmplist.append(tmpdic)
            segdic['lines'] = tmplist
            result.append(segdic)
        dic['result'] = result

        return render_to_response("lyricstructure/ruc_name1.html",dic)
